﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using KAImage.Algorithms;

namespace IPCPhotoEditor
{
    public partial class FormHistograms : Form
    {
        private bool _bEnableUpdated = true;
        public bool EnableUpdated
        {
            set { _bEnableUpdated = value; }
            get { return _bEnableUpdated; }
        }

        private ImageHistograms _histograms = ImageHistograms.GetInstance();

        private bool _bExistHistogram = false;

        public FormHistograms()
        {
            InitializeComponent();
            comboBoxChannels.SelectedIndex = 0;
        }

        public void BrowsedImage_Updated(object sender, BrowsedImageUpdatedEventArgs e)
        {
            if (_bEnableUpdated)
            {              
                ShowHistograms(e.NewImage);               
            }
        }

        private void ClearHistograms()
        {
            histogramControlAll.ClearHistogram();
            histogramControlR.ClearHistogram();
            histogramControlG.ClearHistogram();
            histogramControlB.ClearHistogram();
        }

        public void ShowHistograms(Bitmap source)
        {
            _bExistHistogram = false;
            if (source == null)
            {
                ClearHistograms();
                return;
            }
            try
            {           
                _histograms.CalculateHistograms(source);             
                _bExistHistogram = true;
                comboBoxChannels_SelectedIndexChanged(null, null);
                histogramControlR.UpdateHistogram(_histograms, ColorChannel.R);
                histogramControlG.UpdateHistogram(_histograms, ColorChannel.G);
                histogramControlB.UpdateHistogram(_histograms, ColorChannel.B);
            }
            catch(Exception ex)
            {
                MessageBox.Show("更新直方图发生了异常:" + ex.Message);
            }
        }

        private void comboBoxChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_bExistHistogram)
                return;
            switch (comboBoxChannels.SelectedIndex)
            {
                case 0:
                    histogramControlAll.HistogramColor = Color.FromArgb(255, 220, 220, 220);
                    histogramControlAll.UpdateHistogram(_histograms, ColorChannel.RGB);
                    break;              
                case 1:
                    histogramControlAll.HistogramColor = Color.Red;
                    histogramControlAll.UpdateHistogram(_histograms, ColorChannel.R);
                    break;
                case 2:
                    histogramControlAll.HistogramColor = Color.Green;
                    histogramControlAll.UpdateHistogram(_histograms, ColorChannel.G);
                    break;
                case 3:
                    histogramControlAll.HistogramColor = Color.Blue;
                    histogramControlAll.UpdateHistogram(_histograms, ColorChannel.B);
                    break;
                case 4:
                    histogramControlAll.UpdateHistogram(_histograms);
                    break;
                default:
                    break;
            }
        }
    }
}
